from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import os

# 配置腾讯云 COS
secret_id = "AKIDYy95mGWXIo69wTs3yd5KK4auzoOjpfGc"  # 替换为你的SecretId
secret_key = "81f4FBtMbbLq7AELNDznU0LSL1SecNs9"  # 替换为你的SecretKey
region = "ap-hongkong"  # 替换为你的存储桶地域，例如 'ap-shanghai'
bucket = "hcecpod-1343049753"  # 替换为你的存储桶名称

# 初始化 COS 客户端
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
client = CosS3Client(config)

# 指定本地要上传的文件夹
local_folder = "D:/Desk/222"  # 修改为你的本地文件夹路径
cos_folder = "test/"  # 存储桶中的文件夹路径

def upload_file(local_file_path, cos_path):
    """上传单个文件到腾讯云 COS"""
    with open(local_file_path, 'rb') as fp:
        response = client.put_object(
            Bucket=bucket,
            Body=fp,
            Key=cos_path,
            StorageClass='STANDARD'
        )
    print(f"上传成功: {local_file_path} -> {cos_path}")

def batch_upload(local_folder, cos_folder):
    """批量上传文件到 COS"""
    for root, _, files in os.walk(local_folder):
        for file in files:
            local_file_path = os.path.join(root, file)
            relative_path = os.path.relpath(local_file_path, local_folder)
            cos_path = os.path.join(cos_folder, relative_path).replace("//", "/")
            upload_file(local_file_path, cos_path)

# 运行批量上传
batch_upload(local_folder, cos_folder)
